package base.day08;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class study02 {
    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setTitle("聊天室");
        jf.setSize(400, 300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);

        //聊天窗口
        JTextArea historyrecord = new JTextArea();
        historyrecord.setBounds(10, 10, 360, 200);
        jf.add(historyrecord);
        //编辑信息文本框
        JTextField textField = new JTextField();
        textField.setBounds(10, 230, 180, 20);
        jf.add(textField);
        //发送按钮
        JButton sentbutton = new JButton("发送");
        sentbutton.setBounds(200, 230, 70, 20);
        jf.add(sentbutton);
        //清空按钮
        JButton cleanbutton = new JButton("清空聊天");
        cleanbutton.setBounds(280, 230, 100, 20);
        jf.add(cleanbutton);
        sentbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String message = textField.getText();
                message = message.trim();
                textField.setText("");
                historyrecord.append(message + "\n");
            }
        });
        cleanbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                historyrecord.setText("");
            }
        });


        jf.setVisible(true);
    }
}
